package com.thiege.mall.service.impl;

import com.thiege.mall.common.constant.RespMessage;
import com.thiege.mall.common.dto.ServerResponse;
import com.thiege.mall.dao.IUserDao;
import com.thiege.mall.domain.User;
import com.thiege.mall.service.IUserService;
import com.thiege.mall.util.MD5DigestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户服务接口实现类
 * Created by wang on 2017/5/31.
 */
@Service(value = "iUserService")
public class IUserServiceImpl implements IUserService {

    private final IUserDao iUserDao;

    @Autowired
    public IUserServiceImpl(IUserDao iUserDao) {
        this.iUserDao = iUserDao;
    }

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 用户密码
     * @return 登录结果
     */
    @Override
    public ServerResponse<User> userLogin(String username, String password) {
        //密码加密
        String md5Password = MD5DigestUtil.getMD5Password(password);
        User user = iUserDao.selectUserByUsernameAndPassword(username, md5Password);
        if (user == null)
            //不存在返回错误信息
            return ServerResponse.newInstanceOfFailure(RespMessage.user.USERNAMEORPASSWORD_ERROR); //用户名或者密码错误
        else{
            //存在用户则置空密码返回
            user.setPassword(StringUtils.EMPTY);
            return ServerResponse.newInstanceOfSuccess(user);
        }

    }
}
